package com.kcwx.common.utils.http;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson2.JSON;

public class HttpUtils2 {
	private static final Logger logger = LoggerFactory.getLogger(HttpUtils2.class);

	public static String doGet(String url, String token1, String token2) {
		String result = null;
		CloseableHttpClient httpclient = null;
		try {
			httpclient = HttpClients.createDefault();
			HttpGet httpGet = new HttpGet(url);
			httpGet.addHeader("Accept", "*/*");
			// response.addHeader("access-token", accessToken);
			// response.addHeader("x-access-token", accessToken2);
			httpGet.setHeader("access-token", token1);
			httpGet.setHeader("x-access-token", token2);
			CloseableHttpResponse closeableHttpResponse = httpclient.execute((HttpUriRequest) httpGet);
			int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
			if (statusCode == 200) {
				result = EntityUtils.toString(closeableHttpResponse.getEntity(), "utf-8");
			} else {
				result = statusCode + "";
			}
			httpclient.close();
		} catch (IOException e) {
			logger.error(e.getMessage());
		} finally {
			if (httpclient != null)
				try {
					httpclient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return result;
	}

	public static String doPost(String url, Map<String, ?> paramMap) {
		String result = null;
		CloseableHttpClient httpclient = null;
		try {
			httpclient = HttpClients.createDefault();
			HttpPost httpPost = new HttpPost(url);

			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000)
					.setConnectionRequestTimeout(500).setSocketTimeout(10000).setRedirectsEnabled(true).build();
			httpPost.setConfig(requestConfig);

			httpPost.setHeader("Content-Type", "application/json");
			httpPost.addHeader("Connection", "Keep-Alive");

			httpPost.setEntity((HttpEntity) new StringEntity(JSON.toJSONString(paramMap), Charset.forName("UTF-8")));
			CloseableHttpResponse closeableHttpResponse = httpclient.execute((HttpUriRequest) httpPost);
			int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
			if (statusCode == 200) {
				result = EntityUtils.toString(closeableHttpResponse.getEntity(), "utf-8");
			}
			httpclient.close();
		} catch (IOException e) {
			logger.error(e.getMessage());
		} finally {
			if (httpclient != null)
				try {
					httpclient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return result;
	}

	/**
	 * POST形式发送对象信息
	 * 
	 * @param param
	 * @param url
	 * @param stringBuffer
	 * @return
	 */
	public static String doPostBody(String url, Object param) {
		String result = null;
		CloseableHttpClient httpclient = null;
		try {
			httpclient = HttpClients.createDefault();
			HttpPost httpPost = new HttpPost(url);

			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000)
					.setConnectionRequestTimeout(500).setSocketTimeout(10000).setRedirectsEnabled(true).build();
			httpPost.setConfig(requestConfig);

			httpPost.setHeader("Content-Type", "application/json");
			httpPost.addHeader("Connection", "Keep-Alive");

			httpPost.setEntity((HttpEntity) new StringEntity(JSON.toJSONString(param), Charset.forName("UTF-8")));
			CloseableHttpResponse closeableHttpResponse = httpclient.execute((HttpUriRequest) httpPost);
			int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
			if (statusCode == 200) {
				result = EntityUtils.toString(closeableHttpResponse.getEntity(), "utf-8");
			}
			httpclient.close();
		} catch (IOException e) {
			logger.error(e.getMessage());
		} finally {
			if (httpclient != null)
				try {
					httpclient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return result;
	}

	/**
	 * POST形式发送对象信息
	 * 
	 * @param param
	 * @param url
	 * @param stringBuffer
	 * @return
	 */
	public static String doPostData(String url, String param) {
		String result = null;
		CloseableHttpClient httpclient = null;
		try {
			httpclient = HttpClients.createDefault();
			HttpPost httpPost = new HttpPost(url);

			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000)
					.setConnectionRequestTimeout(500).setSocketTimeout(10000).setRedirectsEnabled(true).build();
			httpPost.setConfig(requestConfig);

			httpPost.setHeader("Content-Type", "application/json");
			httpPost.addHeader("Connection", "Keep-Alive");

			httpPost.setEntity((HttpEntity) new StringEntity(param, Charset.forName("UTF-8")));
			CloseableHttpResponse closeableHttpResponse = httpclient.execute((HttpUriRequest) httpPost);
			int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
			if (statusCode == 200) {
				result = EntityUtils.toString(closeableHttpResponse.getEntity(), "utf-8");
			}
			httpclient.close();
		} catch (IOException e) {
			logger.error(e.getMessage());
		} finally {
			if (httpclient != null)
				try {
					httpclient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return result;
	}
}
